import React from 'react';
import {StyleSheet, View} from 'react-native';
import Toast from 'react-native-easy-toast';

import api from '../../../common/api';

import Top from './top';
import StatisticsMonth from './statisticsMonth';
import StatisticsDay from './statisticsDay';

export default class extends React.Component {
    state = {
        balance: 0,                                     //账户余额
        pre_month_price: 0,                             //本月预估收入
        pre_month_price_end: 0,                         //上个月已结算
        present_month_price: 0,                         //上个月预估收入
        price_end: 0,                                   //累计结算收益
        today_order_num: 0,                             //今日订单笔数
        today_price: 0,                                 //今日预估收入
        yesterday_order_num: 0,                         //昨日订单笔数
        yesterday_price: 0                              //昨日预估收入
    };

    //获取数据
    getData() {
        api({url: 'api/member/getUserIncomeInfo'})
            .then(data => {
                const {balance, pre_month_price, pre_month_price_end, present_month_price, price_end, today_order_num, today_price, yesterday_order_num, yesterday_price} = data;
                this.setState({
                    balance,
                    pre_month_price,
                    pre_month_price_end,
                    present_month_price,
                    price_end,
                    today_order_num,
                    today_price,
                    yesterday_order_num,
                    yesterday_price
                });
            })
            .catch(e => {
                this.showToast(e.msg);
            });
    }

    //toast弹出
    showToast = (text, time = 1000) => {
        this.refs.toast.show(text, time);
    };

    componentDidMount() {
        this.getData();
    }

    render() {
        const {navigation} = this.props;
        const {balance, pre_month_price, pre_month_price_end, present_month_price, price_end, today_order_num, today_price, yesterday_order_num, yesterday_price} = this.state;
        return (
            <View style={styles.container}>
                <Top navigation={navigation} balance={balance} price_end={price_end}/>
                <StatisticsMonth pre_month_price={pre_month_price}
                                 pre_month_price_end={pre_month_price_end}
                                 present_month_price={present_month_price}/>
                <StatisticsDay title="今日" num={today_order_num} price={today_price}/>
                <StatisticsDay title="昨日" num={yesterday_order_num} price={yesterday_price}/>
                <Toast ref="toast" position="center"/>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        backgroundColor: '#f0f0f0',
        flex: 1
    }
});
